package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Point;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.PointMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.ShopingCarQueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PointMapper pointMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public User get(Long authorId) {
        return userMapper.selectByPrimaryKey(authorId);
    }

    @Override
    public void follows(Long theirsId) {
        User user=UserContext.getCurrentUser();
        userMapper.insertFollow(user.getId(),theirsId);
        userMapper.insertFans(user.getId(),theirsId);
    }

    @Override
    public void cancelFollow(Long theirsId) {
        User user=UserContext.getCurrentUser();
        userMapper.deleteFollow(user.getId(),theirsId);
        userMapper.deleteFans(user.getId(),theirsId);
    }

    @Override
    public List<Long> getFansByUserId(Long id) {
        return userMapper.selectFansByUserId(id);
    }

    @Override
    public List<Long> getFollowByUserId(Long id) {
        return userMapper.selectFollowByUserId(id);
    }


    @Override
    public PageInfo queryProducts(ShopingCarQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.selectForProducts();
        return new PageInfo(list);
    }




    @Override
    public void saveOrUpdate(User user) {
        if (user.getId() != null) {
            userMapper.updateByPrimaryKey(user);
            UserContext.setCurrentUser(user);
        }else{
            //如果没有输入邮箱参数则抛异常
            User temp = userMapper.selectEmailAndPassword(user.getEmail(), null);
            //根据此邮箱用户已存在则无法保存  报异常
            if (temp != null) {
                throw new RuntimeException("邮箱已存在");
            }
            user.setHeadImgUrl("/upload/5b43c87c-ba4c-48aa-bbff-19d5972c806c.jpeg");
            user.setCoverImgUrl("/upload/7e1e1d7c-cdeb-455a-af66-32d021a77138.jpeg");
            userMapper.insert(user);
            //创建新的积分记录
            Point point = new Point();
            point.setUser(user);
            pointMapper.insert(point);

        }
    }

    @Override
    public User login(User user) {
        User temp = userMapper.selectEmailAndPassword(user.getEmail(), user.getPassword());
        if (temp == null) {
            throw new RuntimeException("账号或者密码不存在");
        }
        UserContext.setCurrentUser(temp);
        return temp;
    }

    @Override
    public boolean checkEmail(String email) {
        return userMapper.selectEmail(email)==null? true : false;
    }
}
